package Tool;

import static androidx.core.content.ContextCompat.startActivity;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Vibrator;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AppTool {
    /**
     * @param
     * @return
     * @Title: unicodeDecode
     * @Description: unicode解码
     */
    public static String unicodeDecode(String string) {
        Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
        Matcher matcher = pattern.matcher(string);
        char ch;
        while (matcher.find()) {
            ch = (char) Integer.parseInt(matcher.group(2), 16);
            string = string.replace(matcher.group(1), ch + "");
        }
        return string;
    }

    /**
     * @param time
     * @param context
     *///震动函数
    public static void vibSet(long time, Context context) {
        Vibrator vib = (Vibrator) context.getSystemService(Service.VIBRATOR_SERVICE);
        vib.vibrate(time);//只震动一秒，一次
    }

    /**
     * @param str
     * @return boolean
     *///判断
    public static boolean isNumeric(String str) {
        return str != null && str.chars().allMatch(Character::isDigit);
    }

    /**
     * 判断字符串中是否包含中文
     *
     * @param str 待校验字符串
     * @return 是否为中文
     * @warn 不能校验是否为中文标点符号
     */
    public static boolean isContainsChinese(String str) {
        if (str == null) {
            return false;
        }
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        return m.find();
    }

    /**
     * @param str
     * @return {@link String}
     */
    public static String trunnumString(String str) {
        String regEx = "[^0-9]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        String result = m.replaceAll("").trim();
        return result;
    }

    /**
     * @param input
     * @return {@link JSONObject}
     * @throws JSONException
     *///str转json
    public static JSONObject CalStrToJSON(String input) throws JSONException {
        if (input == null || input.equals("")) return null;
//        String pattern = "\\(([^\\)]+)\\)";
//        Matcher matcher = Pattern.compile(pattern).matcher(input);
//        if (!matcher.find()) return null;
//        String output = matcher.group(1);
        String output=input.substring(input.indexOf("({")+1,input.indexOf("}")+1);
//        Log.d("结果:", output);
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(output);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject;
    }

    /**
     * @return boolean
     */
    public static boolean DeterminesIfAStringHasASubstring(String Str, String str) {
        if (Str.indexOf(str) >= 0) {
            return true;
        } else
            return false;
    }

    public static String GetTime() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        return formatter.format(date);
    }

    /**
     * 判断是否打开网络定位
     *
     * @param context 用于获取LocationManager
     * @return true/false
     */
    public static boolean isOpenGps(Context context) {
        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        if (locationManager != null) {
            //这里只判断了是否开启网络定位
            //还有GPS定位            LocationManager.GPS_PROVIDER
            return locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        } else {
            return false;
        }
    }

    /**
     * @param time
     * @return {@link String}
     *///计算时间
    public static String CalTime(Double time) {
        if (Double.isNaN(time)) {
            time = 0.0;
        }
        SimpleDateFormat format = new SimpleDateFormat("dd天HH小时mm分钟");
        String date = format.format(time);//注意这里返回的是string类型
        return date;

//        if (time <= 100) {
//            DecimalFormat df = new DecimalFormat("#0.00");
//            String str = df.format(time);
//            return str + " 分钟";
//        } else if (time <= 24 * 60) {
//            DecimalFormat df = new DecimalFormat("#0.00");
//            String str = df.format(time / 60);
//            return str + " 小时";
//        } else {
//            DecimalFormat df = new DecimalFormat("#0.00");
//            String str = df.format(time / (60 * 24));
//            return str + " 天";
//        }

    }

    /**
     * @param use
     * @return {@link String}
     *///计算使用
    public static String CalUse(Double use) {
        if (Double.isNaN(use)) {
            use = 0.0;
        }
        if (use < (1024 * 1024)) {
            DecimalFormat df = new DecimalFormat("#0.00");
            String str = df.format(use / 1024);
            return str + " MB";
        } else {
            DecimalFormat df = new DecimalFormat("#0.00");
            String str = df.format(use / (1024 * 1024 * 1024));
            return str + " GB";
        }
    }


}
